﻿<MToolbar Dark
          Color="teal">
    <MToolbarTitle>State selection</MToolbarTitle>
    <MAutocomplete @bind-Value="_select"
                   Loading="_loading"
                   Items="_items"
                   OnSearchInputUpdate="QuerySelections"
                   CacheItems
                   Class="mx-4"
                   Flat
                   HideNoData
                   ItemValue="r => r"
                   ItemText="r => r"
                   HideDetails="true"
                   Label="What state are you from?"
                   SoloInverted>
    </MAutocomplete>
    <MButton Icon>
        <MIcon>mdi-dots-vertical</MIcon>
    </MButton>
</MToolbar>

@code {

    private static List<string> _states = new List<string>()
    {
        "Alabama", "Alaska", "American Samoa", "Arizona",
        "Arkansas", "California", "Colorado", "Connecticut",
        "Delaware", "District of Columbia", "Federated States of Micronesia",
        "Florida", "Georgia", "Guam", "Hawaii", "Idaho",
        "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
        "Louisiana", "Maine", "Marshall Islands", "Maryland",
        "Massachusetts", "Michigan", "Minnesota", "Mississippi",
        "Missouri", "Montana", "Nebraska", "Nevada",
        "New Hampshire", "New Jersey", "New Mexico", "New York",
        "North Carolina", "North Dakota", "Northern Mariana Islands", "Ohio",
        "Oklahoma", "Oregon", "Palau", "Pennsylvania", "Puerto Rico",
        "Rhode Island", "South Carolina", "South Dakota", "Tennessee",
        "Texas", "Utah", "Vermont", "Virgin Island", "Virginia",
        "Washington", "West Virginia", "Wisconsin", "Wyoming"
    };

    private bool _loading;
    private string? _select;
    private List<string> _items = new List<string>();

    private async Task QuerySelections(string v)
    {
        if (string.IsNullOrEmpty(v) || v == _select)
        {
            return;
        }

        _loading = true;
        await Task.Delay(500);
        _items = _states.Where(e => e.Contains(v, StringComparison.OrdinalIgnoreCase)).ToList();
        _loading = false;
    }

}